// pages/search/search.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        searchField: "",
        lostlist: new Array(),
        lostShow: new Array(),
    },
    onChange(event){
        // console.log(event);
        this.setData({searchField: event.detail.value});
    },
    gotoDetailPublish(event){
        var lostId = event.target.dataset.id;
        // console.log(lostId);
        wx.navigateTo({
          url: '/pages/detailPublish/detailPublish?lostId=' + lostId + '&whereFrom=myPublish',
        })
    },
    doNothing(){

    },
    onSearch(){
        wx.request({
            url: app.globalData.URL_PRE + '/searchByName?searchField=' + this.data.searchField,
            method: 'GET',
            header:{
                'cookie': wx.getStorageSync('cookie'),
            },
            success: (res)=>{
                // 成功
                if(res.data.code === 200){
                    // 数据为空
                    if(res.data.data == "" || res.data.data == null){
                        // console.log(res.data);
                        wx.showToast({
                            // todo 修改
                          title: '没有查询到该人',
                          icon: 'error',
                          duration: 1500
                        })
                    }
                    // 成功请求
                    else{
                      console.log(res.data.data);
                        this.setData({
                          lostlist: res.data.data,
                          lostShow: res.data.data
                        })
                    }
                }
            }
          })
    },
    tuijianOnchange(event){
      // console.log(event);
      var nextType = event.detail.name;
      if(nextType === "tuijian"){// 推荐
        console.log("推荐");
        this.changeTuiJian();
      }
      if(nextType === "age"){//年龄
        console.log("年龄");
        this.changeAge(0, 20);
      }
      if(nextType === "gender"){//姓别
        console.log("姓别");
        this.changeGender("男");
      }
      
      if(nextType === "area"){//地区
        console.log("地区");
        this.changeTuiJian();
      }
    },
    genderOnchange(event){
      console.log(event);
      var nextType = event.detail.name;
      if(nextType === "boy"){// 男孩
        console.log("男");
        this.changeGender("男");
      }
      if(nextType === "girl"){//女孩
        console.log("女");
        this.changeGender("女");
      }
    },

    ageOnchange(event){
      console.log(event);
      var nextType = event.detail.name;
      if(nextType === 0){// 0-20
        console.log("0-20");
        this.changeAge(0, 20);
      }
      if(nextType === 1){//20-40
        console.log("20-40");
        this.changeAge(20, 40);

      }
      if(nextType === 2){// 40-60
        console.log("40-60");
        this.changeAge(40, 60);

      }
      if(nextType === 3){//60+
        console.log("60+");
        this.changeAge(60, 200);

      }
    },
    changeGender(gender){
      var losts = new Array();
      if(this.data.lostlist === null) return;
      for(let i = 0; i < this.data.lostlist.length; i++){
        const obj = this.data.lostlist[i];
        // console.log(obj);
        if(obj.gender === gender)
          losts.push(obj);
      }
      losts = this.modifyIndex(losts);
      console.log(losts);
      // console.log(this.data.lostlist);
      this.setData({
        lostShow: losts
      })
    },
    changeAge(ageMin, ageMax){
      var losts = new Array();
      if(this.data.lostlist === null) return;
      var nowDate = new Date();
      var nowYear = nowDate.getFullYear();
      // console.log(nowYear);
      for(let i = 0; i < this.data.lostlist.length; i++){
        const obj = this.data.lostlist[i];
        // console.log(obj);
        let peopleYear = parseInt(obj.birthday.substring(0, 4))
        // console.log(peopleYear);
        if(peopleYear + ageMin <= nowYear && peopleYear + ageMax >= nowYear)
          losts.push(obj);
      }
      losts = this.modifyIndex(losts);
      console.log(losts);
      // console.log(this.data.lostlist);
      this.setData({
        lostShow: losts
      })
    },
    changeTuiJian(){
      var losts = this.data.lostlist;
      losts = this.modifyIndex(losts);
      this.setData({
        lostShow: losts
      })
    },


    modifyIndex(losts){
      for(let i = 0; i < losts.length; i++){
        losts[i].index = i +1;
      }
      return losts;
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // console.log(options);
        this.setData({searchField: options.searchField});
        this.onSearch();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        wx.setNavigationBarTitle({
            title: '搜索',
          })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})